package models.contract;

import models.UUIDModel;
import models.constants.DeletedStatus;
import models.enums.AuthorizationStatus;
import models.enums.BankCardType;
import models.user.User;

import javax.persistence.*;

/**
 * 银行卡信息
 */
@Entity
@Table(name = "co_bank_cards")
public class BankCard extends UUIDModel {

    /**
     *  银行保存的 该银行卡的 uuid 信息
     *  后期通过该信息反向获取  银行卡信息
     *  平台不存储 银行卡信息
     */
    @Column(name = "bank_uuid", length = 20)
    public String bankUUId;

    /**
     * 银行卡名称
     */
    @Column(name = "card_name", length = 20)
    public String bankName;

    /**
     * 银行卡 卡号
     */
    @Column(name = "bank_card_number", length = 20)
    public String bankCardNumber;

    /**
     * 绑定银行卡的用户
     * 将来可能用于发送账单
     */
    @JoinColumn(name = "user_id")
    @ManyToOne
    public User user;

    /**
     * 多张卡对应一个合同
     */
    @JoinColumn(name = "contract_id")
    @ManyToOne
    public Contract contract;

    /**
     * 银行卡类型
     */
    @Enumerated(EnumType.STRING)
    @Column(name = "bank_card_type", length = 10)
    public BankCardType bankCardType;

    /**
     * 是否授权代扣
     */
    @Enumerated(EnumType.STRING)
    @Column(name = "withhold_authorization", length = 15)
    public AuthorizationStatus withholdAuthorization;

    /**
     * 根据合同查找指定的银行卡
     * @param contract
     * @return
     */
    public static BankCard findByContract(Contract contract){
        return BankCard.find("contract =?1 and deleted =?2" , contract , DeletedStatus.UN_DELETED).first();
    }

}
